#!/bin/sh
export JAVA_HOME=/usr/local/jdk

gitUrl=$1
gitBranch=$2
sysName=$3
serverIP=$4
appDir=$5
[ ! -n "$appDir" ] && appDir=${sysName}-service/target
#warName=$6
warName=$6
[ ! -n "$warName" ] && warName=${sysName}-service
codeDir=${sysName}.ewt360.com
sourceDir=/opt/source
logsDir=/opt/logs
warSrcDir=$sourceDir/$codeDir/$appDir

warDestDir=/opt/war
remotedestdir=/home/newwar
if [ ! -d "$sourceDir/$codeDir" ]; then
    cd $sourceDir && git clone $gitUrl $codeDir 2>&1|tee $logsDir/${sysName}_gitcode.log
else
    cd $sourceDir/$codeDir && git pull 2>&1|tee $logsDir/${sysName}_gitcode.log
fi
cd $sourceDir/$codeDir && git checkout $gitBranch 2>&1|tee -a $logsDir/${sysName}_gitcode.log

cd $sourceDir/$codeDir && /usr/local/apache-maven/bin/mvn clean install -Dmaven.test.skip=true 2>&1|tee $logsDir/${sysName}_mvn.log
cd $warSrcDir && cp -rf ${warName}.war $warDestDir

servers=`awk 'BEGIN{list="'${serverIP}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
for server in ${servers[*]}
do
    scp $warDestDir/${warName}.war root@$server:$remotedestdir 2>&1|tee $logsDir/${sysName}_${server}.log
done

